import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:gm_login_plugin/gm_login_plugin.dart';
import 'package:riki_login/config/riki_platform_config.dart';
import 'package:riki_login/entity/login_arg.dart';
import 'package:riki_login/res/header.dart';
import 'user_login_third_auth.dart';

///作者：范玉辉
///创建时间：2021/4/2
///说明：登陆管理

class GmExConfigCtx extends RikiLoginPlatformConfig {
  GmExConfigCtx()
      : super(
          "app-dabanjia|ver-v${RikiLogin.instance.version}|plt-aPhone|AppStore",
          "app-dabanjia|ver-v${RikiLogin.instance.version}|plt-iPhone|AppStore",
        );
}

class LoginManager {
  /// 私有构造函数
  LoginManager._internal();

  /// 保存单例
  static LoginManager _loginManager = new LoginManager._internal();

  /// 工厂构造函数
  factory LoginManager() => _loginManager;

  /// 登录SDK初始化,应放到启动函数中调⽤
  void initEnviriment() async {
    String ctx = GmExConfigCtx().value;

    //版本配置，请修改为集成方自己的版本，SDK根据改版本屏蔽三方登录列表
    String userAgentVersion = RikiLogin.instance.version;
    // 基础配置
    GLoginSDKBaseConfig exConfig = GLoginSDKBaseConfig(
      ctx: ctx,
      userAgentName: RikiLogin.instance.version,
      userAgentVersion: userAgentVersion,
    );

    if (Platform.isIOS) {
      // iOS 第三方登陆
      exConfig.supportPlatforms = [
        GMESurpportPlatform.wechart,
        GMESurpportPlatform.apple,
        // GMESurpportPlatform.qq,
      ];
      // 所集成APP
      exConfig.environmentType = GMELoginInitEnvironmentType.Decorate;
      // 初始化微信sdk及添加授权监听事件
      LoginThirdAuth.initThirdSDK();
    } else {
      // 安卓微信AppId
      exConfig.weixinAppId = RikiLogin.instance.wechatAppId;
    }

    //配置SDK
    GLoginCustomConfiguration config = GLoginCustomConfiguration();
    // 加密证书路径
    config.loginCertPath = 'assets/${RikiLogin.instance.cerName}.der';
    // appid
    config.loginAppID = RikiLogin.instance.gmAppId;

    exConfig.isProductEnv = RikiLogin.instance.isDebug;

    // 是否使用第三方
    config.isEnableThirdLogin = true;
    // 不显示logo下文案
    config.isShowGomeAccountTips = false;

    // UI配置
    config.logoImagePath = R.libAssetsIcLoginLogo;
    config.buttonNormallImagePath = R.libAssetsIcLoginDbjbtnBlueBgNormal;
    config.buttonDisableImagePath = R.libAssetsIcLoginDbjbtnBlueBgDisable;
    config.buttonNormalTextColor = "#F7F7F7";
    config.buttonDisableTextColor = "#FFFFFF";
    config.agreeSelectedIconPath = R.libAssetsIcLoginAgreeSelectedIcon;
    config.agreeUnselectedIconPath = R.libAssetsIcLoginAgreeUnselectedIcon;
    config.buttonVerificationNormalColor = "#35C4C4";
    config.buttonVerificationHighlightBgColor = "#30B0B0";
    config.buttonVerificationHighlightTextColor = "#FFFFFF";
    config.buttonVerificationDisableColor = "#9AE1E1";
    config.defaultGlobalColor = "#35C4C4";

    //使用exCongif 初始化SDK
    GMLoginPlugin.initEnviriment(exConfig, config);
  }

  // 弹出登录页面
  Future<bool> login(BuildContext context) async {
    return await GMLoginPlugin.login();
  }

  // 自动登录
  Future<bool> autoLogin() async => await GMLoginPlugin.autoLogin();

  // 退出登录, 返回退出登录状态
  Future<bool> logOut() async {
    if (RikiLogin.instance.developLogin) {
      // 如果是开发者
      return true;
    }
    return await GMLoginPlugin.logOut();
  }

  // 获取登录状态
  Future<bool?> getLoginStatus() async => await GMLoginPlugin.getLoginStatus();

  // 获取登录信息
  Future<GMMLoginInfo> getLoginInfo() async => await GMLoginPlugin.getLoginInfo();

  Future<String?> getSdkVersion() async => await GMLoginPlugin.getSdkVersion();
}
